﻿using UnityEngine;
using UnityEditor;
using UnityEditor.AnimatedValues;
using System.Linq;
using UnityEditor.IMGUI.Controls;
using System.Collections.Generic;

namespace CWM.Skinn
{
    [CustomEditor(typeof(BlendshapeController))]
    public partial class BlendshapeControllerInspector : Editor
    {
        public AnimBool showShapes;

        private const float fadeSpeed = 5f;

        public void OnEnable()
        {
            if (showShapes == null) showShapes = new AnimBool(false);
            showShapes.valueChanged.RemoveListener(Repaint);
            showShapes.valueChanged.AddListener(Repaint);
            showShapes.speed = fadeSpeed;
        }

        private static GUIContent label = new GUIContent("Child Blend-shapes", "Updates OnEnable or from the Context Menu.");
        ///private static Color grey = new Color(1f, 1f, 1f, 0.1f);

        private SearchField searchField = null;

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Separator();

            var rectWidth = GUILayoutUtility.GetLastRect().width;
            var component = target as BlendshapeController; if (!component) return;

            showShapes.target = EditorGUILayout.Foldout(showShapes.target, label, true);

            if (showShapes.faded > 0)
            {
                component.blendableElements.UpdateAssets(component.gameObject);
                var elements = component.blendableElements.elements;

                using (new EditorGUILayout.FadeGroupScope(showShapes.faded))
                {
                    if (searchField == null) searchField = new SearchField();
                    EditorGUILayout.LabelField("");
                    component.serachString = searchField.OnGUI(GUILayoutUtility.GetLastRect(), component.serachString);

                    if (SkinnEx.IsNullOrEmpty(elements))
                    {
                        showShapes.target = false;
                    }
                    else
                    {

                        //var drawColor = false;

                        for (int i = 0; i < elements.Count; i++)
                        {
                            if (elements[i].ID == -1) continue;

                            if (!string.IsNullOrEmpty(component.serachString))
                            {
                                if (!elements[i].Name.Contains(component.serachString)) continue;
                            }

                            if (!string.IsNullOrEmpty(component.settings.hiddenPrefix))
                            {
                                if (elements[i].Name.StartsWith(component.settings.hiddenPrefix)) continue;
                            }

                            if (!string.IsNullOrEmpty(component.settings.hiddenTag))
                            {
                                if (elements[i].Name.Contains(component.settings.hiddenTag)) continue;
                            }

                            //drawColor = !drawColor;

                            EditorGUILayout.BeginHorizontal();
                            {
                                EditorGUILayout.SelectableLabel(elements[i].Name, GUILayout.MaxHeight(15f));

                                //if (drawColor) EditorGUI.DrawRect(GUILayoutUtility.GetLastRect(), grey);

                                var updateElement = false;

                                EditorGUI.BeginChangeCheck();

                                var weight = elements[i].weight;

                                if (component.settings.unclampShapes)
                                    weight = EditorGUILayout.Slider(weight, component.settings.min, component.settings.max);
                                else
                                    weight = EditorGUILayout.Slider(weight, 0f, 100f);
                                if (EditorGUI.EndChangeCheck())
                                {
                                    updateElement = true;
                                }

                                if (GUILayout.Button("x", EditorStyles.miniButton))
                                {
                                    updateElement = true;
                                    weight = 0f;
                                }

                                if (updateElement)
                                {
                                    var recordObjects = new List<Component>() { component };
                                    foreach (var index in elements[i].indices2)
                                    {

                                        if (index.x >= component.blendableElements.SkinnedMeshes.Length) continue;
                                        if (component.blendableElements.SkinnedMeshes[index.x])
                                            recordObjects.Add(component.blendableElements.SkinnedMeshes[index.x]);
                                    }

                                    Undo.RecordObjects(recordObjects.ToArray(), "Set BlendShape Value");

                                    elements[i].weight = weight;
                                    component.blendableElements.UpdateBlendshape(elements[i]);
                                }
                            }
                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
            }
        }
    }
}